package com.pansoft.androidalldemo.demo1.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.jaeger.library.StatusBarUtil;
import com.pansoft.androidalldemo.R;
import com.pansoft.androidalldemo.constant.Constants;
import com.pansoft.androidalldemo.viewholder.MyViewHolder;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.pansoft.androidalldemo.constant.Constants.abs;
/**
 * 参见网站 http://blog.csdn.net/gdutxiaoxu/article/details/52858598
 */

public class Demo1_3 extends AppCompatActivity {

    @Bind(R.id.tabs)
    TabLayout tabs;
    @Bind(R.id.viewpager)
    ViewPager viewpager;
    @Bind(R.id.main_content)
    CoordinatorLayout mainContent;
    private static final String TAG = "Demo1_2";
    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StatusBarUtil.setTranslucent(this);
        // StatusBarUtil.setColor(this, ContextCompat.getColor(this,R.color.colorPrimary));
        setContentView(R.layout.activity_demo1_3);
        ButterKnife.bind(this);

        tabs.setupWithViewPager(viewpager);

            toolbar.setTitle("相立博");
        viewpager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return 3;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return Constants.strings.get(position);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                FrameLayout view = new FrameLayout(Demo1_3.this);

                if (position == 0) {
                    view.setBackgroundColor(Color.parseColor("#194945"));
                } else if (position == 1) {

                    view.setBackgroundColor(Color.parseColor("#F65676"));
                } else {
                    view.setBackgroundColor(Color.parseColor("#D0DFE6"));

                }


                RecyclerView recyclerView = new RecyclerView(Demo1_3.this);
                view.addView(recyclerView);
                recyclerView.setLayoutManager(new LinearLayoutManager(Demo1_3.this));
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(new RecyclerView.Adapter() {
                    @Override
                    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                        View view = getLayoutInflater().inflate(R.layout.recycleview_item, null);
                        MyViewHolder myViewHolder = new MyViewHolder(view);
                        return myViewHolder;
                    }

                    @Override
                    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

                        ((MyViewHolder) holder).tv.setText(abs[position] + "");
                    }

                    @Override
                    public int getItemCount() {
                        return abs.length;
                    }
                });

                recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                    }

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                    }
                });

                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
    }
}